package com.nuanshui.heatedloan.task.failover.strategy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

import com.nuanshui.heatedloan.task.failover.TaskFailMessage;
import com.nuanshui.heatedloan.task.failover.TaskfailoverStrategyType;

/**
 * 错误执行策略工厂
 * @param 
 * @author guohao
 * @date 2017年2月13日 上午10:08:29
 * @since $
 */
public class TaskFailStrategyFactory  extends SpringBeanJobFactory implements ApplicationContextAware{

	private transient AutowireCapableBeanFactory beanFactory;
	 
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		 beanFactory = applicationContext.getAutowireCapableBeanFactory();		
	}
	
	
	public TaskFailStrategy createTaskFailStrategy(TaskFailMessage taskFailMessage) {
		   TaskFailStrategy taskFailStrategy = new TaskFailDefaultStrategy();
		   if(TaskfailoverStrategyType.RETRY.equals(taskFailMessage.getFailover())) {
			   taskFailStrategy = new TaskFailRetryStrategy(taskFailMessage.getRetries());
			   
		   }
		   beanFactory.autowireBean(taskFailStrategy);
		   return taskFailStrategy;	
	}
	
	

}
